package com.storeapp.ui;


import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import com.storeapp.entity.domain.DealOrder;
import com.storeapp.entity.domain.Item;
import com.storeapp.service.DealOrderService;
import com.storeapp.util.SpringUtils;
import com.storeapp.ui.compator.ItemTableCompator;

public class ItemsTable extends ViewPart implements ISelectionListener {
	public static final String ID = "com.storeapp.ui.ItemsTable";

	private TableViewer tv;
	private ItemTableCompator comparator;
	private List<Item> items;
	private DealOrderService dealOrderService;

	public ItemsTable() {
		super();
		this.items = new ArrayList<Item>();
		dealOrderService = (DealOrderService) SpringUtils
				.getApplicationContext().getBean("dealOrderService");
	}

	@Override
	public void createPartControl(Composite parent) {
		GridLayout layout = new GridLayout(1, false);
		parent.setLayout(layout);
		Label searchLabel = new Label(parent, SWT.NONE);
		searchLabel.setText("Search: ");
		final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
		searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
				| GridData.HORIZONTAL_ALIGN_FILL));
		createViewer(parent);
		comparator = new ItemTableCompator();
		tv.setComparator(comparator);
		
		getSite().getPage().addSelectionListener(OrderList.ID, (ISelectionListener)this);
		
	}
	
	private void createViewer(Composite parent) {
		tv = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
		createColumns(parent, tv);
		final Table table = tv.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		// set the content for the viewer, setInput will call getElements in the
		tv.setContentProvider(new ArrayContentProvider());
		// contentProvider
		tv.setInput(items);
		// Make the selection available to other views
		getSite().setSelectionProvider(tv);

		// Layout the viewer
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		tv.getControl().setLayoutData(gridData);
	}
	
	// This will create the columns for the table
	private void createColumns(final Composite parent, final TableViewer viewer) {
		String[] titles = { "商品名称", "供货商", "单价", "数量", "折扣", "质保" };
		int[] bounds = { 100, 100, 100, 100, 100, 100 };

		//First column is for the itemName
		TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				Item item = (Item) element;
				return item.getItemName();
			}
		});
		
		
		//Second column is for provider
		col = createTableViewerColumn(titles[1], bounds[1], 1);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				Item item = (Item) element;
				return item.getProvider();
			}
		});
		
		//Price
		col = createTableViewerColumn(titles[2], bounds[2], 2);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				Item item = (Item) element;
				return String.valueOf(item.getPrice());
			}
		});
		
		//Quantity
		col = createTableViewerColumn(titles[3], bounds[3], 3);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				Item item = (Item) element;
				return String.valueOf(item.getQuantity());
			}
		});
		
		//Discount
		col = createTableViewerColumn(titles[4], bounds[4], 4);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				Item item = (Item) element;
				return String.valueOf(item.getDiscount()*100.00)+"%";
			}
		});
		
		//Guarantee
		col = createTableViewerColumn(titles[5], bounds[5], 5);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				Item item = (Item) element;
				return item.getGuarentee();
			}
		});
		
	}
	
	private TableViewerColumn createTableViewerColumn(String title, int bound,
			final int colNumber) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(tv,
				SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		column.setWidth(bound);
		column.setResizable(true);
		column.setMoveable(true);
		column.addSelectionListener(getSelectionAdapter(column, colNumber));
		return viewerColumn;

	}
	
	private SelectionAdapter getSelectionAdapter(final TableColumn column,
			final int index) {
		SelectionAdapter selectionAdapter = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				comparator.setColumn(index);
				int dir = tv.getTable().getSortDirection();
				if (tv.getTable().getSortColumn() == column) {
					dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
				} else {

					dir = SWT.DOWN;
				}
				tv.getTable().setSortDirection(dir);
				tv.getTable().setSortColumn(column);
				tv.refresh();
			}
		};
		return selectionAdapter;
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		tv.getControl().setFocus();
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		// TODO Auto-generated method stub
		if( selection instanceof StructuredSelection && 
				((StructuredSelection) selection).getFirstElement() instanceof DealOrder){
			DealOrder order = (DealOrder) ((StructuredSelection) selection).getFirstElement();
			items.clear();
			items.addAll(dealOrderService.listItemsByOrder(order));
			tv.refresh();
		}
		
	}
}
